output$plot1 <- renderPlot({
    data<-get(input$data)
    
    W<-as.matrix(odh);colnames(W)<-rownames(W)
    W<-(W>=0 & W<input$hdist)
    W<-W/rowSums(W)
    lw<-mat2listw(W,row.names=rownames(W),style='W')
    
    pers<-matrix(0,nrow=4,ncol=length(year))
    quadrant <- vector(mode="numeric",length=nrow(data))
    
    for(i in 1:length(year)){
        lmoran<-lisa.perm(data[,paste(input$var,year[i],sep='.')],lw,perm=input$perm)
        
        quadrant[lmoran[,3] >0 & lmoran[,1]>0] <- 1 #H-H   
        quadrant[lmoran[,3] <0 & lmoran[,1]>0] <- 2 #L-L
        quadrant[lmoran[,3] <0 & lmoran[,1]<0] <- 3 #L-H     
        quadrant[lmoran[,3] >0 & lmoran[,1]<0] <- 4 #H-L
        quadrant[lmoran[,2]>0.05] <- 0    
        pers[1,i]<-table(quadrant==1)[2]
        pers[2,i]<-table(quadrant==2)[2]
        pers[3,i]<-table(quadrant==3)[2]
        pers[4,i]<-table(quadrant==4)[2]
    }
    
    plot(pers[1,]~year,t='b',pch=17,ylim=c(0,max(pers,na.rm=TRUE)),col='red',
         ylab='',xlab='Year')        
    lines(pers[2,]~year, type='b', pch=17, col='blue')        
    lines(pers[3,]~year, type='b', pch=16, col=rgb(0,0,1,alpha=0.4))        
    lines(pers[4,]~year, type='b', pch=16, col=rgb(1,0,0,alpha=0.4))
    legend('bottomleft',xpd=TRUE,inset=c(-0.1,-0.3),legend=c('HH','LL','LH','HL'),
           bty="n",cex=0.7,y.intersp=1,x.intersp=1,pch=c(17,17,16,16),lty=rep(1,4),
           col=c('red','blue',rgb(0,0,1,alpha=0.4),rgb(1,0,0,alpha=0.4))) ##Note the use of the inset option.
    
})
